package com.meitianhui.convenitenbuy.module.home.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.model.database.SearchHistoryDataBean;

import java.util.List;

/**
 * Created by Administrator on 2017/11/16.
 */

public class HomeHistorySearchAdapter extends BaseQuickAdapter<SearchHistoryDataBean, BaseViewHolder> {
    private Context context;
    private final View footer;
    private List<SearchHistoryDataBean> mData;
    private final View header;
    private boolean exsitHFView = false;

    public HomeHistorySearchAdapter(Context context, @Nullable List<SearchHistoryDataBean> data) {
        super(R.layout.item_home_history_search, data);
        header = View.inflate(context, R.layout.item_home_history_search, null);
        footer = View.inflate(context, R.layout.item_home_history_search_foot, null);
        if(data!=null){
            if (!data.isEmpty()){
                addHeaderView(header);
                addFooterView(footer);
                exsitHFView = true;
            }
        }
        this.context = context;
        mData = data;
        setFooterViewClick();
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchHistoryDataBean item) {
        helper.setText(R.id.tv_home_search_item, item.getHistory());
    }

    public void setFooterViewClick() {
        footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getInstance().getDaoSession().getSearchHistoryDataBeanDao().deleteAll();
                mData.clear();
                removeAllFooterView();
                removeAllHeaderView();
                notifyDataSetChanged();
                exsitHFView = false;
            }
        });
    }

    public void addHeaderAndFooterView() {
        if (!exsitHFView) {
            addHeaderView(header);
            addFooterView(footer);
            exsitHFView = true;
        }
    }

}
